package top.lionkliu.listener;

import lombok.extern.slf4j.Slf4j;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

@WebListener
@Slf4j
public class MySessionListener implements HttpSessionListener, HttpSessionAttributeListener {

    public MySessionListener() {
    }

    @Override
    public void attributeAdded(HttpSessionBindingEvent se) {
        log.info("HttpSessionAttributeListener.attributeAdded()方法开始工作");
        String key = se.getName();
        if (key.equals("username")) {
            log.info("执行了request.getSession().setAttribute(username);方法");
            //从上下文中获取已经登录账号的集合
            List<String> onLineUserList = (List<String>) se.getSession().getServletContext().getAttribute("onLineUserList");
            //在上下文中没有登陆用户
            if (onLineUserList == null || onLineUserList.size() == 0) {
                onLineUserList = new ArrayList<>();
            }
            String username = (String) se.getSession().getAttribute("username");
            //向已登录集合中添加当前账号
            onLineUserList.add(username);
            System.out.println("用户：" + username + " 成功加入在线用户列表");
            for (int i = 0; i < onLineUserList.size(); i++) {
                System.out.println(onLineUserList.get(i));
            }
            se.getSession().getServletContext().setAttribute("onLineUserList", onLineUserList);
        }
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent se) {
        HttpSession session = se.getSession();
        ServletContext application = session.getServletContext();
        List<String> onlineUserList = (List<String>) application.getAttribute("onLineUserList");
        // 取得登录的用户名
        String username = (String) session.getAttribute("username");
        if (!"".equals(username) && username != null && onlineUserList != null && onlineUserList.size() > 0) {
            // 从在线列表中删除用户名
            onlineUserList.remove(username);
            log.info("用户：{} 已经退出！", username);
            log.info("当前在线人数为:{}", onlineUserList.size());
        } else {
            log.info("会话已经销毁！");
        }
    }

    @Override
    public void attributeRemoved(HttpSessionBindingEvent se) {
        log.info("HttpSessionAttributeListener.attributeRemoved()方法开始工作");
        String key = se.getName();
        if (key.equals("username")) {
            log.info("执行了request.getSession().removeAttribute(username);方法");

        }

    }
}
